﻿<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">

    <script type='text/html' template lay-done='layui.data.formdone(d.params);'>
        <input type='hidden' name='PID' value="{{ d.params.PID || '' }}" />
    </script>
    <!-- <div class='layui-form-item'>
     <label class='layui-form-label'>优惠卷编码</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='Code' value='{{ d.params.Code || '' }}' lay-verify='required' placeholder='优惠卷编码' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>-->
    <!-- <div class='layui-form-item'>
          <label class='layui-form-label'>优惠卷类型 <label style="color: red">*</label></label>
          <div class='layui-input-block'>
              <div class='layui-input-inline'>
                  <script type='text/html' template>
                      <select name='Type_Sel' lay-filter='Type_Sel' id='Type_Sel'>
                         <!-- <option value='0' {{ d.params.Type_Sel == '0' ? 'selected' : '' }}>商品优惠卷</option>
                          <option value='1' {{ d.params.Type_Sel == '1' ? 'selected' : '' }}>店铺优惠卷</option>
                          <option value='2' {{ d.params.Type_Sel == '2' ? 'selected' : '' }}>平台优惠卷</option>-->
    <!-- </select>
        </script>
    </div>
                </div>
            </div> -->
    <!--<div class='layui-form-item' id='divstoreSource'>
        <label class='layui-form-label'>指定店铺<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                    <input type='hidden' name='storeSource' id="storeSource" value='{{ d.params.storeSource || '' }}'>
                </script>
        </div>

        <div class="layui-form-mid layui-word-aux">
            选中<span style="color:red" id="storeName">0</span>店铺
            <button type="button" id="Guider" onclick="SelStore()" class="layui-btn layui-btn-xs">选择店铺</button>
        </div>
    </div>
    <div class='layui-form-item' id='divgoodSource'>
        <label class='layui-form-label'>指定商品</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='hidden' name='goodSource' id="goodSource" value='{{ d.params.goodSource || '' }}'>
            </script>
        </div>
        <div class="layui-form-mid layui-word-aux">
            已选中<span style="color:red" id="num">0</span>个商品
            <button type="button" id="Guider" onclick="SelGoods()" class="layui-btn layui-btn-xs">选择商品</button>
        </div>

        <label class='layui-form-label'>选择主题</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <select name='Theme_Sel' id="Theme_Sel"  xm-select="Theme_Sel"  xm-select-search="">
                </select>
            </script>
        </div>


    </div>-->
    <div class='layui-form-item'>
        <label class='layui-form-label'>优惠卷标题<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='Title' value='{{ d.params.Title || '' }}' lay-verify='required' placeholder='优惠卷标题' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>使用条件 <label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <div class='layui-input-inline'>
                <script type='text/html' template>
                    <select name='Use_Sel' lay-verify='required'>
                        <option value='0' {{ d.params.Use_Sel == '0' ? 'selected' : '' }}>满减</option>
                        <option value='1' {{ d.params.Use_Sel == '1' ? 'selected' : '' }}>无门槛</option>
                    </select>
                </script>
            </div>
        </div>
    </div>


    <div class='layui-form-item'>
        <label class='layui-form-label'>有效时间(天)</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='CouponDay' value='{{ d.params.CouponDay || '' }}' lay-verify='number' placeholder='有效时间' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>

    <div class='layui-form-item'>
        <label class='layui-form-label'>门槛金额<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='SillPrice' value='{{ d.params.SillPrice || '' }}' lay-verify='required' placeholder='门槛金额(可以填写0元)' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>优惠卷面额<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='Price' value='{{ d.params.Price || '' }}' lay-verify='required' placeholder='优惠卷面额' autocomplete='off' class='layui-input'>

            </script>
        </div>
    </div>

    <!--<div class='layui-form-item'>
        <label class='layui-form-label'>会员等级限制 </label>
        <div class='layui-input-block'>
            <div class='layui-input-inline'>
                <script type='text/html' template lay-done='layui.data.getSelSource(d.params,"MemberLevelSource");'>
                    <select name='MemberLevelSource' id='MemberLevelSource' xm-select="MemberLevelSource" lxm-select-search="" >
                        <option value='' {{ d.params.IsOpen_Sel == '' ? 'selected' : '' }}>无需会员等级限制</option>
                    </select>
                </script>
            </div>
        </div>
    </div>-->

    <div class='layui-form-item'>
        <label class='layui-form-label'>发放开始时间<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template lay-done='layui.data.datedone(d.params);'>
                <input type='text' name='GrantStartTime' readonly='readonly' id='GrantStartTime' value='{{ d.params.GrantStartTime || '' }}' lay-verify='required|datetime' placeholder='yyyy-MM-dd HH:mm:ss' autocomplete='off' class='layui-input'>
            </script>
        </div>

        <label class='layui-form-label'>发放结束时间<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template lay-done='layui.data.datedone(d.params);'>
                <input type='text' name='GrantEndTime' readonly='readonly' id='GrantEndTime' value='{{ d.params.GrantEndTime || '' }}' lay-verify='required|datetime' placeholder='yyyy-MM-dd HH:mm:ss' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>


    <div class='layui-form-item'>

        <label class='layui-form-label'>发放数量<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='GrantNum' value='{{ d.params.GrantNum || '' }}' lay-verify='required|number' placeholder='发放数量' autocomplete='off' class='layui-input'>
            </script>
        </div>


        <label class='layui-form-label'>限领数量<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='LimitNum' value='{{ d.params.LimitNum || '' }}' lay-verify='required|number' placeholder='限领数量' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>

    <!--<div class='layui-form-item'>
        <label class='layui-form-label'>是否公开</label>
        <div class='layui-input-block'>
            <div class='layui-input-inline'>
                <script type='text/html' template>
                    <select name='IsOpen_Sel'>
                      <option value='0' {{ d.params.IsOpen_Sel == '0' ? 'selected' : '' }}>公开</option>
                      <option value='1' {{ d.params.IsOpen_Sel == '1' ? 'selected' : '' }}>不公开</option>
                    </select>
                </script>
            </div>
        </div>
    </div>-->

    <div class='layui-form-item'>
        <label class='layui-form-label'>生效时间<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template lay-done='layui.data.datedone(d.params);'>
                <input type='text' name='UseStartTime' readonly='readonly' id='UseStartTime' value='{{ d.params.UseStartTime || '' }}' lay-verify='required|datetime' placeholder='yyyy-MM-dd HH:mm:ss' autocomplete='off' class='layui-input'>
            </script>
        </div>

        <label class='layui-form-label'>截止时间<label style="color: red">*</label></label>
        <div class='layui-input-inline'>
            <script type='text/html' template lay-done='layui.data.datedone(d.params);'>
                <input type='text' name='UseEndTime' readonly='readonly' id='UseEndTime' value='{{ d.params.UseEndTime || '' }}' lay-verify='required|datetime' placeholder='yyyy-MM-dd HH:mm:ss' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>


    <!-- <div class='layui-form-item'>
        <label class='layui-form-label'>已领取数量</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='ApplyNum' value='{{ d.params.ApplyNum || '' }}' lay-verify='required|number' placeholder='已领取数量' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div> -->


    <div class='layui-form-item'>
        <label class='layui-form-label'>优惠卷状态 </label>
        <div class='layui-input-block'>
            <div class='layui-input-inline'>
                <script type='text/html' template>
                    <select name='State_Sel'>
                        <option value='0' {{ d.params.State_Sel == '0' ? 'selected' : '' }}>待激活</option>
                        <option value='1' {{ d.params.State_Sel == '1' ? 'selected' : '' }}>激活</option>
                        <option value='2' {{ d.params.State_Sel == '2' ? 'selected' : '' }}>冻结</option>
                        <option value='3' {{ d.params.State_Sel == '3' ? 'selected' : '' }}>回收</option>
                    </select>
                </script>
            </div>
        </div>
    </div>

    <div class='layui-form-item'>
        <label class='layui-form-label'>优惠卷描述</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='Description' value='{{ d.params.Description || '' }}' placeholder='优惠卷描述' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>备注</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <textarea type='text' name='RMK' value='{{ d.params.RMK || '' }}' placeholder='备注' autocomplete='off' class='layui-textarea'>
            </script>
        </div>
    </div>
    <!-- <div class='layui-form-item'>
        <label class='layui-form-label'>二维码地址</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' name='QRCode' value='{{ d.params.QRCode || '' }}' lay-verify='required' placeholder='二维码地址' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div> -->
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" value="确认" class="layui-btn">
        </div>
    </div>
</div>

<script type="text/javascript">
    layui.use(['form', 'admin', 'upload', 'laydate', 'formSelects'], function () {
        var form = layui.form;//required（必填项）phone（手机号）email（邮箱）url（网址）number（数字）date（日期）identity（身份证） 自定义值
        var $ = layui.$;
        var admin = layui.admin;
        $("#divgoodSource").show();
        $("#divstoreSource").hide();
        var formSelects = layui.formSelects;


        formSelects.config('MemberLevelSource', {
            keyName: 'LevelStr',            //自定义返回数据中name的key, 默认 name
            keyVal: 'Level',            //自定义返回数据中value的key, 默认 value
        });

        formSelects.config('Theme_Sel', {
            keyName: 'Name',            //自定义返回数据中name的key, 默认 name
            keyVal: 'PID',            //自定义返回数据中value的key, 默认 value
        });

        form.on('select(Type_Sel)', function (data) {
            selectChange(data.value)
        });

    });


    function selectChange(val) {
        var $ = layui.$;
        if (val == 2) {
            $("#divstoreSource").hide();
            $("#storeSource").val("");
            checkIDs1 = [];
            $("#storeName").text(0);
            $("#divgoodSource").hide();
            $("#goodSource").val("");
            checkIDs = [];
            $("#num").val(0);
        }

        if (val == 1) {
            $("#divstoreSource").show();
            $("#goodSource").val("");
            checkIDs = [];
            $("#num").text(0);
            $("#divgoodSource").hide();
        }

        if (val == 0) {
            $("#divgoodSource").show();
            $("#divstoreSource").hide();
            $("#storeSource").val("");
            checkIDs1 = [];
            $("#storeName").text(0);
        }
    }


    layui.data.formdone = function (d) {

        layui.use(['form', 'admin', 'upload', 'laydate', 'formSelects'], function () {
            var $ = layui.$;
            var admin = layui.admin;
            var form = layui.form;

            var formSelects = layui.formSelects;
            selectChange(d.Type_Sel);

            var goodSource = d.GoodSource;

            admin.req({
                url: layui.setter.APIUrl + '/shop/ThemeMgr/customerQuery', //实际使用请改成服务端真实接口
                type: 'post',
                done: function (res) {
                    // var html = "<option value=''></option>";
                    // layui.each(res.Data, function (index, eachdata) {
                    //     html += "<option value='" + eachdata.PID + "'>" + eachdata.Name + "</option>";
                    // });
                    // $("#Theme_Sel").html(html);
                    // form.render('select');

                    layui.formSelects.data('Theme_Sel', 'local', {
                        arr: res.Data,
                    });
                    
                    if (d.ThemeId) {
                        if (d.ThemeId.indexOf(',') == -1) {
                            layui.formSelects.value('Theme_Sel', [d.ThemeId]);
                        } else {
                            layui.formSelects.value('Theme_Sel', d.ThemeId.split(','));
                        }
                    }
                }
            });



            if (goodSource != "" && goodSource != null && goodSource != "[]") {
                var gids = goodSource.split(',')
                $("#num").text(gids.length);
                checkIDs = gids;
                $("#goodSource").val(goodSource);
            }

            var storeSource = d.StoreSource;
            if (storeSource != "" && storeSource != null && storeSource != "[]") {
                var gids = storeSource.split(',')
                $("#storeName").text(gids.length);
                checkIDs1 = gids;
                $("#storeSource").val(storeSource);
            }

            admin.req({
                url: layui.setter.APIUrl + '/WebAPI/user/userInfo', //实际使用请改成服务端真实接口
                type: 'post',
                done: function (res) {
                    var Data = [{ value: 0, Name: '平台优惠卷' }/*, { value: 1, Name: '店铺优惠卷' }, { value: 2, Name: '商品优惠卷' }*/]
                    layui.each(Data, function (index, eachdata) {
                        var strIsSel = '';
                        if (eachdata.value == d.Type_Sel)
                            strIsSel = "selected='selected'";
                        $("#Type_Sel").append("<option value='" + eachdata.value + "' " + strIsSel + ">" + eachdata.Name + "</option>");

                        // if(!d.Type_Sel)
                        // {
                        //     $("#Type_Sel").val(0);
                        // }
                    });

                    form.render('select');
                }
            });

        })

    }


    function SelGoods() {
        var view = layui.view;
        var admin = layui.admin;
        var $ = layui.$;
        admin.popup({
            title: '商品列表'
            , skin: 'layui-layer-lan'
            , area: ['85%', '96%']
            , zIndex: 1002
            , shadeClose: false
            , id: 'LAY-popup-content-add2'
            , success: function (layero, index) {
                lindex = index;
                view(this.id).render('/shop/shop_coupon/selectgood').done(function () {
                    //form.render(null, 'layuiadmin-app-form-list');
                    var active = {
                        confirm: function () {
                            // if (checkIDs.length === 0) {
                            //     return layer.msg('请选择数据');
                            // }
                            var count = checkIDs.length;
                            $("#num").text(count);
                            var goodids = checkIDs.join(',');//JSON.stringify(checkIDs);
                            $("#goodSource").val(goodids);
                            layer.close(lindex);
                        },
                    };

                    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                });
            }
        });
    }


    function SelStore() {
        var view = layui.view;
        var admin = layui.admin;
        var $ = layui.$;


        admin.popup({
            title: '店铺列表'
            , skin: 'layui-layer-lan'
            , area: ['85%', '96%']
            , zIndex: 1004
            , shadeClose: false
            , id: 'LAY-popup-content-add3'
            , success: function (layero, index) {
                lindex = index;
                view(this.id).render('/shop/shop_coupon/selectStore').done(function () {
                    //form.render(null, 'layuiadmin-app-form-list');
                    var active = {
                        confirm: function () {
                            if (checkIDs1.length === 0) {
                                return layer.msg('请选择数据');
                            }
                            var count = checkIDs1.length;
                            $("#storeName").text(count);
                            var goodids = checkIDs1.join(',');//JSON.stringify(checkIDs1);
                            $("#storeSource").val(goodids);

                            layer.close(lindex);
                        },
                    };

                    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                });
            }
        });

    }

    layui.data.getSelSource = function (d, sourceName) {
        layui.use(['admin', 'form', 'formSelects'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var formSelects = layui.formSelects;

            if (sourceName == "MemberLevelSource") {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/Goods_memberpriceMgr/getLevels', //实际使用请改成服务端真实接口
                    type: 'post',
                    done: function (res) {
                        layui.formSelects.data('MemberLevelSource',
                            'local',
                            {
                                arr: res.Data, //要把自己的这个节点去掉
                                linkeage: false,
                                linkageWidth: 140
                            });

                        if (d.MemberLevelSource) {
                            if (d.MemberLevelSource.indexOf(',') == -1) {
                                layui.formSelects.value('MemberLevelSource', [d.MemberLevelSource]);
                            } else {
                                layui.formSelects.value('MemberLevelSource', d.MemberLevelSource.split(','));
                            }
                        }

                        //我也不知道出了啥问题 在页面第一次进来的时候 就是显示不对 所以在这个地方重新再选择一下
                        selectChange(d.Type_Sel);
                    }
                });
            }
        });
    }


    layui.data.setUpload = function (d) {
        layui.use(['upload', 'admin'], function () {
            var $ = layui.$,
                upload = layui.upload,
                admin = layui.admin;
            var formSelects = layui.formSelects;
        });
    }


    layui.data.datedone = function (d) {
        layui.use(['laydate'], function () {
            var laydate = layui.laydate;

            //日期时间选择器
            laydate.render({
                elem: '#GrantStartTime'
                , type: 'datetime'
                , calendar: true
            });
            laydate.val = d.GrantStartTime;

            //日期时间选择器
            laydate.render({
                elem: '#GrantEndTime'
                , type: 'datetime'
                , calendar: true
            });
            laydate.val = d.GrantEndTime;

            //日期时间选择器
            laydate.render({
                elem: '#UseStartTime'
                , type: 'datetime'
                , calendar: true
            });
            laydate.val = d.UseStartTime;

            //日期时间选择器
            laydate.render({
                elem: '#UseEndTime'
                , type: 'datetime'
                , calendar: true
            });
            laydate.val = d.UseEndTime;

        });
    }   
</script>